#ifndef __UPGRADE_ACM32_H__
#define __UPGRADE_ACM32_H__

#include "intf_usb.h"

#define ACM32FP_V                   "ACM32FP_P1_V"

#define ACM32_FW_FILE_SIZE_MAX  (1*1024*1024)
#define ACM32_FW_FILE_SIZE_MIN  (10*1024)


#define ACM32_BOOT_USB_VID 0x1234
#define ACM32_BOOT_USB_PID 0xabce

#define ACM32_BOOT_TRANSFER_TIMEOUT 1000

#define ACM32_BOOT_DOWNLOAD_DATA_LEN_MAX   240

#define ACM32_TRANSFER_DELAY()     usleep(2 * 1000)

typedef enum _ACM32_DOWNLOAD_CMD_ENUM
{
    ACM32_CMD_SELF_TEST = 0xC7,
    ACM32_CMD_SECURITY_CODE = 0xCC,
    ACM32_CMD_ERASE_EFLASH = 0xD3,
    ACM32_CMD_DOWNLOAD_CODE_TO_EFLASH = 0xD7,
    ACM32_CMD_CHECK_CRC = 0xD8,
    ACM32_CMD_DOWNLOAD_CRC_VALUE = 0xD9,
    ACM32_CMD_RUN_APP = 0xE3,
    ACM32_CMD_READ_SN = 0xA0,
    ACM32_CMD_READ_EFLASH = 0xD0,
    ACM32_CMD_ENABLE_WRITE_PROTECT = 0xD2,
    ACM32_CMD_DISABLE_WRITE_PROTECT = 0xD5,
    ACM32_CMD_ENABLE_READ_PROTECT = 0xDA,
    ACM32_CMD_DISABLE_READ_PROTECT = 0xD4,
    ACM32_CMD_RESET = 0xCB,
    ACM32_CMD_READ_CRC = 0xA6,
}ACM32_DOWNLOAD_CMD_ENUM;


#define ACM32_UP_USB_BUFFER_SIZE   1024

/*协议包数据结构*/
typedef struct _ACM32_UP_PROT_PACKET_
{
    uint8  prot_state;          //串口协议解析状态
    uint8  prot_head;
    uint8  prot_type;
    uint16 prot_size;
    uint8  prot_data[ACM32_UP_USB_BUFFER_SIZE];  //保存上位机的参数数据
    uint32 prot_data_cnt;       //对数据长度进行计数
    uint8  prot_bcc;
    uint8  prot_end;
    //uint16 prot_checksum;

    uint8  prot_respond;        //给host的应答
    uint8  prot_source;         //数据从UART或SPI过来的,UART:0 ;SPI:1

    uint8  current_cmd_code;    //当前发送的指令码, add by hcq, 20230117
    uint8  current_resp_cmd;    //当前应答的命令
    uint8  current_resp_type;   //当前应答结果
    //uint8  recv_prot_type;
}ACM32_UP_PROT_PACKET;

/*标识包处理状态机*/
enum
{
    ACM32_UP_PACKET_IDLE = 0,
    ACM32_UP_PACKET_HEAD,
    ACM32_UP_PACKET_TYPE,
    ACM32_UP_PACKET_SIZE1,
    ACM32_UP_PACKET_SIZE2,
    ACM32_UP_PACKET_DATA,
    ACM32_UP_PACKET_BCC,
    ACM32_UP_PACKET_END,
    ACM32_UP_PACKET_RECV_SUCCESS,
    //PACKET_CHECKSUM1,
    //PACKET_CHECKSUM2,
    ACM32_UP_PACKET_ERROR = 100,  //包出错
};

typedef struct _ACM32_UPGRADE_PARAM_
{
    INTF_USB_HANDLE_STRUCT usb_handle;
    INTF_USB_ID_STRUCT usb_id;

    //protocol
    ACM32_UP_PROT_PACKET prot_packet_usb;   //报文协议处理结构
    uint8  usb_recv_data[ACM32_UP_USB_BUFFER_SIZE];
    uint16 usb_recv_cnt;

    uint8 dev_sn[64];
    uint16 crc_value_dev;
    uint16 crc_value_file;
}ACM32_UPGRADE_PARAM;


extern int32 acm32_upgrade_module_init(ACM32_UPGRADE_PARAM *this);
extern int32 acm32_upgrade_and_run(ACM32_UPGRADE_PARAM *this, char *file_path);



#endif

